﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WriteBinaryFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            textBox1.Text = folderBrowserDialog1.SelectedPath;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                MessageBox.Show("请选择文件路径");
                return;
            }

            if (String.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                MessageBox.Show("请设置文件名称");
                return;
            }

            try
            {
                FileStream myStream = new FileStream(textBox1.Text + "\\" + textBox2.Text+".bin", FileMode.Create);
                BinaryWriter myWriter = new BinaryWriter(myStream);
                for (int i = 0; i < 10; i++)
                {
                    myWriter.Write(i);
                }
                myWriter.Close();
                myStream.Close();
                MessageBox.Show("创建并写入成功！");
            }
            catch(Exception ex) 
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
